import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login'
import register from "../views/register";
import sellHome from '../views/SellHome'
import sellList from "../views/background/sellList";

import userHome from "../views/userHome";
import adminHome from "../views/adminHome";


import ShopCart from "../views/user/ShopCart";
import Index from "../views/user/Index";
import Order from "../views/user/Order";

import normal_user from "../views/admin/normaluser";
import sell_user from "../views/admin/selluser";
import PerCenter from "../views/user/PerCenter";
import SellOrder from "../views/background/SellOrder";
import center from "../views/background/center";
Vue.use(VueRouter)


const routes = [
    {
        path: '/login',
        name: 'login',
        component: login
    },
    {
        path: '/register',
        name: 'register',
        component: () => {
            return import("../views/register.vue");
        }
    },
    {
        path: '/userHome',
        name: "userHome",
        component: userHome,
        children:[
            {
                path : '/Index',
                component: Index,
                name: 'Index',
            },
            {
                path : '/PerCenter',
                component: PerCenter,
                name: 'PerCenter',
            },
            {
                path : '/ShopCart',
                component: ShopCart,
                name: 'ShopCart',
            },
            {
                path : '/Order',
                component: Order,
                name: 'Order',
            },

        ]
    },
    {
        path: '/admin',
        name: "adminHome",
        component: adminHome,
        children:[
            {
                path : '/normal_user',
                component: normal_user,
                name: 'normal_user',
            },
            {
                path : '/sell_user',
                component: sell_user,
                name: 'sell_user',
            },
        ]
    },
    {
        path : '/sellHome',
        component: sellHome,
        children:[
            {
                path : '/sellList',
                component: sellList,
                name: 'list_db',
            },
            {
                path : '/SellOrder',
                component: SellOrder,
                name: 'SellOrder',
            },
            {
                path : '/center',
                component: center,
                name: 'center',
            },
        ]
    },


]

const router = new VueRouter({
    routes
})
// /   判断是否有token(当直接访问的不是登陆页面时)
router.beforeEach((to, from, next)=>{
    if(to.path === '/login' || to.path === '/register'){
        next();
    }
    else{
        let token = window.sessionStorage.getItem('token'); ///获取浏览器的token
        if(!token || token == ''||token == null || token == undefined){ ////如果不存在
            next('/login');
        }
        next();
    }
})


export default router
